package com.doubao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.doubao.entity.SmartDeviceTag;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;

@Mapper
public interface SmartDeviceTagMapper extends BaseMapper<SmartDeviceTag> {

    /**
     * 查询智能体的所有标签
     */
    @Select("SELECT t.* FROM smart_device_tag t " +
            "INNER JOIN smart_device_tag_relation r ON r.tag_id = t.id " +
            "WHERE r.device_id = #{deviceId}")
    List<SmartDeviceTag> selectDeviceTags(@Param("deviceId") Long deviceId);

    /**
     * 查询标签使用次数
     */
    @Select("SELECT COUNT(*) FROM smart_device_tag_relation WHERE tag_id = #{tagId}")
    int selectTagUseCount(@Param("tagId") Long tagId);

    /**
     * 查询热门标签
     */
    @Select("SELECT t.*, COUNT(r.id) as use_count " +
            "FROM smart_device_tag t " +
            "LEFT JOIN smart_device_tag_relation r ON r.tag_id = t.id " +
            "GROUP BY t.id " +
            "ORDER BY use_count DESC " +
            "LIMIT #{limit}")
    List<SmartDeviceTag> selectHotTags(@Param("limit") Integer limit);
}
